/* global emptyFunc */

/**
 * 获取推广海报
 * @param callback
 * @param loading
 */
function doGetRecommPoster(callback, loading) {
    var url = $.domain + "/Recomm/doGetRecommPoster";
    var data = {};
    sApi(url, data, callback, loading);
}

/**
 * 获取用户取现记录
 * @param page
 * @param callback
 * @param loading
 */
function doGetWithdrawApplyList(page, callback, loading) {
    var url = $.domain + "/Withdraw/doGetWithdrawApplyList";
    var data = {
        "page": page ? page : 1
    };
    sApi(url, data, callback, loading);
}

/**
 * 申请取现
 * @param name
 * @param income
 * @param callback
 * @param loading
 */
function doApplyWithdraw(name, income, callback, loading) {
    var url = $.domain + "/Withdraw/doApplyWithdraw";
    var data = {
        "name": name,
        "income": income
    };
    sApi(url, data, callback, loading);
}

/**
 * 获取徒弟列表
 * @param page
 * @param callback
 * @param loading
 */
function doGetRecommList(page, callback, loading) {
    var url = $.domain + "/Recomm/doGetRecommList";
    var data = {
        "page": page ? page : 1
    };
    sApi(url, data, callback, loading);
}

/**
 * 获取徒弟已完成的任务列表
 * @param {type} page
 * @param {type} userId
 * @param {type} callback
 * @param {type} loading
 * @returns {undefined}
 */
function doGetRecommerTaskList(page, userId, callback, loading) {
    var url = $.domain + "/Recomm/doGetRecommerTaskList";
    var data = {
        "page": page ? page : 1,
        "userId": userId
    };
    sApi(url, data, callback, loading);
}

/**
 * 完成任务
 * @param id
 * @param data
 * @param callback
 * @param loading
 */
function doFinishJoinTask(id, data, callback, loading) {
    var url = $.domain + "/Join/doFinishJoinTask";
    data = $.extend(data, {
        "id": id
    });
    sApi(url, data, callback, loading);
}

/**
 * 获取参与任务的用户列表
 * @param id
 * @param page
 * @param state
 * @param callback
 * @param loading
 */
function doGetJoinTaskUserList(id, page, state, callback, loading) {
    var url = $.domain + "/Join/doGetJoinTaskUserList";
    var data = {
        "id": id,
        "page": page,
        "state": state
    };
    sApi(url, data, callback, loading);
}

/**
 * 加入一个任务
 * @param id
 * @param callback
 * @param loading
 */
function doJoinTask(id, callback, loading) {
    var url = $.domain + "/Join/doJoinTask";
    var data = {
        "id": id
    };
    sApi(url, data, callback, loading);
}

/**
 * 取消用户未完成任务
 * @param taskType
 * @param callback
 * @param loading
 */
function doCancelUnfinishedJoinTask(taskType, callback, loading) {
    var url = $.domain + "/Join/doCancelUnfinishedJoinTask";
    var data = {
        "taskType": taskType
    };
    sApi(url, data, callback, loading);
}

/**
 * 获取当前用户未完成任务数
 * @param taskType
 * @param callback
 * @param loading
 */
function doGetUnfinishedTaskCount(taskType, callback, loading) {
    var url = $.domain + "/Join/doGetUnfinishedTaskCount";
    var data = {
        "taskType": taskType
    };
    sApi(url, data, callback, loading);
}

/**
 * 获取已开始未结束的任务列表
 * @param taskType
 * @param page
 * @param callback
 * @param loading
 */
function doGetTaskList(taskType, page, callback, loading) {
    var url = $.domain + "/Task/doGetTaskList";
    var data = {
        "taskType": taskType,
        "page": page ? page : 1
    };
    sApi(url, data, callback, loading);
}

/**
 * 获取用户参与的任务列表
 * @param page
 * @param state
 * @param callback
 * @param loading
 */
function doGetUserTaskList(page, state, callback, loading) {
    var url = $.domain + "/User/doGetUserTaskList";
    var data = {
        "page": page,
        "state": state
    };
    sApi(url, data, callback, loading);
}

/**
 * 根据短信code更新手机号码
 * @param code
 * @param callback
 * @param loading
 */
function doUpdataUserPhone(code, callback, loading) {
    var url = $.domain + "/User/doUpdataUserPhone";
    var data = {
        "code": code
    };
    sApi(url, data, callback, loading);
}


/**
 * 获取绑定手机的短信验证码
 * @param phone
 * @param imgCode
 * @param callback
 * @param loading
 */
function doGetBindPhoneSmsCode(phone, imgCode, callback, loading) {
    var url = $.domain + "/User/doGetBindPhoneSmsCode";
    var data = {
        "phone": phone,
        "imgCode": imgCode
    };
    sApi(url, data, callback, loading);
}

/**
 * 编辑用户数据
 * @param {*} data 
 * @param {*} callback 
 * @param {*} loading 
 */
function doEditUser(data, callback, loading) {
    var url = $.domain + "/User/doEditUser";
    sApi(url, data, callback, loading);
}

/**
 * 提交设备号
 * @param {*} data 
 * @param {*} callback 
 * @param {*} loading 
 */
function doSaveDeviceCode(deviceCode, callback, loading) {
    var url = $.domain + "/GatheringTour/goGatheringTourIndex";
    var data={
        deviceCode : deviceCode
    }
    sApi(url, data, callback, loading);
}

/**
 * api调用
 * @param url
 * @param data
 * @param callback
 * @param loading
 */
function sApi(url, data, callback, loading) {
    if (typeof callback !== "function") {
        callback = emptyFunc;
    }

    if (typeof loading == "undefined") {
        loading = true;
    }

    var param = {
        "url": url,
        "data": data,
        "success": function (res) {
            if (!res) {
                res = errRes("网络响应超时");
            }
            callback(res);
        },
        "error": function () {
            callback(errRes("网络响应超时"));
        },
        "loading": loading ? true : false
    };
    $.sAjax(param);
}